package com.gitee.wsl.android.ui.spannable

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.style.TypefaceSpan
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import androidx.annotation.IntRange
import androidx.core.text.set
import com.gitee.wsl.android.ui.spannable.span.BorderSpan
import com.gitee.wsl.android.ui.spannable.span.BubbleBackgroundSpan
import com.gitee.wsl.android.ui.spannable.span.CodeSizeSpan
import com.gitee.wsl.android.ui.spannable.span.HeaderSpan
import com.gitee.wsl.android.ui.spannable.span.RoundedBackgroundSpan
import com.gitee.wsl.android.ui.spannable.span.SpaceSpan
import com.gitee.wsl.android.ui.spannable.span.StrikeSpan
import com.gitee.wsl.android.ui.spannable.span.TextTagSpan
import com.gitee.wsl.android.ui.spannable.span.TypefaceSpanCompat
import com.gitee.wsl.android.ext.ApplicationInit.application
import com.gitee.wsl.android.ext.SpannableReplaceAtScope
import com.gitee.wsl.android.ext.SpannableScope
import com.gitee.wsl.android.ext.SpannableStringBuilderExt
import com.gitee.wsl.android.ext.backgroundColor
import com.gitee.wsl.android.ext.newLineAble


fun SpannableScope.typeface(
    typeface: Typeface,
    builderAction: SpannableScope.() -> Unit
): SpannableScope = inSpans(TypefaceSpanCompat(typeface), builderAction)

fun SpannableReplaceAtScope.typeface(
    typeface: Typeface){
    spannableScope[startIndex, endIndex] = TypefaceSpanCompat(typeface)
}

fun SpannableScope.appendSpace(
    @FloatRange(from = 0.0) size: Float,
    @ColorInt color: Int = Color.TRANSPARENT
): SpannableScope = appendSpace(size.toInt(), color)

fun SpannableScope.appendSpace(
    @IntRange(from = 0) size: Int,
    @ColorInt color: Int = Color.TRANSPARENT
): SpannableScope = inSpans(SpaceSpan(size, color)) { append(SpannableStringBuilderExt.SPACE_SPAN_TEXT) }

//段落
inline fun SpannableScope.inSection(
    builderAction: SpannableScope.() -> Unit
): SpannableScope {
    newLineAble()
    builderAction()
    newLineAble()
    appendSpace(2)
    return this
}
fun SpannableScope.roundedBackground(
    backgroundColor: Int = 0,
    textColor: Int = 0,
    cornerRadius: Int = 8,
    builderAction: SpannableScope.() -> Unit
): SpannableScope = inSpans(RoundedBackgroundSpan(backgroundColor,textColor,cornerRadius), builderAction)

fun SpannableReplaceAtScope.roundedBackground(
    backgroundColor: Int = 0,
    textColor: Int = 0,
    cornerRadius: Int = 8){
    spannableScope[startIndex, endIndex] = RoundedBackgroundSpan(backgroundColor,textColor,cornerRadius)
}

fun SpannableScope.bubbleBackground(
    builderAction: SpannableScope.() -> Unit
): SpannableScope = inSpans(BubbleBackgroundSpan(), builderAction)

fun SpannableReplaceAtScope.bubbleBackground(){
    spannableScope[startIndex, endIndex] = BubbleBackgroundSpan()
}

fun SpannableScope.border(
    color:Int=Color.BLUE,
    builderAction: SpannableScope.() -> Unit
): SpannableScope = inSpans(BorderSpan(color), builderAction)

fun SpannableReplaceAtScope.border(color:Int=Color.BLUE){
    spannableScope[startIndex, endIndex] = BorderSpan(color)
}

fun SpannableScope.strike(
    strokeWidth: Int,
    builderAction: SpannableScope.() -> Unit
): SpannableScope = inSpans(StrikeSpan(strokeWidth), builderAction)

fun SpannableReplaceAtScope.strike(strokeWidth: Int){
    spannableScope[startIndex, endIndex] = StrikeSpan(strokeWidth)
}

fun SpannableScope.textTag(
    context: Context=application,
    width: Int,
    height: Int,
    builderAction: SpannableScope.() -> Unit
): SpannableScope = inSpans(TextTagSpan(context,width,height), builderAction)

fun SpannableReplaceAtScope.textTag(
    context: Context=application,
    width: Int,
    height: Int){
    spannableScope[startIndex, endIndex] = TextTagSpan(context,width,height)
}

fun SpannableScope.header(
    level: Int,
    builderAction: SpannableScope.() -> Unit
): SpannableScope = inSpans(HeaderSpan.header(level), builderAction)

fun SpannableReplaceAtScope.header(level: Int){
    spannableScope[startIndex, endIndex] = HeaderSpan.header(level)
}

fun SpannableReplaceAtScope.inlineCode(codeTextBgColor:Int=Color.BLACK){
    spannableScope[startIndex, endIndex] = TypefaceSpan("monospace")
    spannableScope[startIndex, endIndex] = CodeSizeSpan(0.8f)
    backgroundColor(codeTextBgColor)
}